package com.capgemini.poste.metier;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


import com.capgemini.poste.dao.Event;
import com.capgemini.poste.dao.Securite;

public class Hotfix extends com.capgemini.poste.dao.Securite {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4093101059780084008L;
	static HashSet<Securite> securites = new HashSet<Securite>() ;
	static int maxNb = 0 ;
	
	public Hotfix ( EntityManager em ) {
		
		// Create the query and populate the HashSet with workstations retrieved from DTB
		Query getAllPoste = em.createQuery("select s from Securite s") ;
		@SuppressWarnings("unchecked")
		List<Securite> results = getAllPoste.getResultList() ;
		for ( Securite result : results ) {
			if ( result.getNb() >= maxNb ) {
				maxNb = result.getNb() ;
			}
			securites.add(result) ;
		}

	}

	public ArrayList<Event> retrieve (XSSFSheet excelSheet ) {
		// TODO Auto-generated method stub
		XSSFRow row;
		boolean bFirst = true ;
		ArrayList<Event> events = new ArrayList<Event>() ;

		for (Iterator<Row> rowIterator = excelSheet.rowIterator(); rowIterator.hasNext();) {
			row = (XSSFRow) rowIterator.next();
            // Skip the header row
			if ( bFirst == true) {
               row = (XSSFRow) rowIterator.next();
               bFirst = false ;
            }
			
			Securite securite = new Securite ();
			// Colonne 3 = Code du poste
			if ( row.getCell(3).getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){
				securite.setCd(String.valueOf(row.getCell(3).getNumericCellValue()));
			}
			else {	
				securite.setCd(row.getCell(3).getStringCellValue().toUpperCase());
			}
				
			// Use a HashSet avoid multiple records
			if ( securites.contains(securite) == false ) {
				securite.setNb(++maxNb) ;
				if ( securites.add(securite) == true ) {
					System.out.println ( "Signature " + securite.getCd() + " has been added !!");
					Event event = new Event () ;
					event.setType(POSTE.HOTFIX) ;
					event.setCd(securite.getCd()) ;
					events.add(event) ;
	
				}
			}

		}
		return events ;
	}

	public ArrayList<Event> retrieve (XSSFWorkbook excelWorkbook ) {
		return this.retrieve(excelWorkbook.getSheetAt(1)) ;
	
	}
	
	public int getSize ( ) {
		return securites.size() ;
	}
	
	public void save ( EntityManager em ) {
		for ( Iterator<Securite> p = securites.iterator(); p.hasNext() ;) {
			em.persist(p.next()) ;
		}
	}
	
	public Securite get ( String Cd ) {
		Securite securite = new Securite () ;
		
		for ( Iterator<Securite> p = securites.iterator(); p.hasNext() ;) {
			securite = p.next() ;
			if ( securite.getCd().equals(Cd) == true ) {
				break ;
			}
		}		
		
		return securite ;
	}

}
